<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="never">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="部门名称" prop="deptName">
              <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="全部" clearable>
                <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button v-hasPermi="['budget:dept:add']" type="primary" plain icon="Plus" @click="handleAdd()">新增 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table border ref="deptTableRef" v-loading="loading" :style="tableStyle" :data="deptList" row-key="deptId" class="enhanced-table">
        <el-table-column prop="deptCode" label="部门编号" align="center" width="150" />
        <el-table-column prop="deptName" label="部门名称" align="center" width="400" show-overflow-tooltip />
        <el-table-column prop="isBusiness" label="是否参与业务预算" align="center">
          <template #default="scope">
            <el-switch
              v-model="scope.row.isBusiness"
              @change="updateStatus(scope.row.deptId, 1, scope.row.isBusiness)"
              style="--el-switch-on-color: #8470FF; --el-switch-off-color: #ff4949"
              size="small"
            />
          </template>
        </el-table-column>
        <el-table-column prop="isManage" label="是否参与管理预算" align="center">
          <template #default="scope">
            <el-switch
              v-model="scope.row.isManage"
              @change="updateStatus(scope.row.deptId, 2, scope.row.isManage)"
              style="--el-switch-on-color: #8470FF; --el-switch-off-color: #ff4949"
              size="small"
            />
          </template>
        </el-table-column>
        <el-table-column prop="status" align="center" label="状态" width="140">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="170">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="center" label="操作">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['budget:dept:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)" />
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['budget:dept:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)" />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <el-dialog v-model="dialog.visible" :title="dialog.title" destroy-on-close append-to-body style="width: 500px">
      <el-form ref="deptFormRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="部门编号" prop="deptCode" label-width="100px">
              <el-input v-model="form.deptCode" placeholder="请输入部门编号" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="部门名称" prop="deptName" label-width="100px">
              <el-input v-model="form.deptName" placeholder="请输入部门名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参与业务预算" label-width="100px">
              <el-radio-group v-model="form.isBusiness">
                <el-radio-button :value="true">是</el-radio-button>
                <el-radio-button :value="false">否</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参与管理预算" label-width="100px">
              <el-radio-group v-model="form.isManage">
                <el-radio-button :value="true">是</el-radio-button>
                <el-radio-button :value="false">否</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="部门状态" label-width="100px">
              <el-radio-group v-model="form.status">
                <el-radio-button v-for="dict in sys_normal_disable" :key="dict.value" :value="dict.value">{{ dict.label }} </el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="budgetDept" lang="ts">
import { addDept, delDept, getDept, listDept, updateDept, updateStatusById } from '@/api/budget/budgetDept';
import { DeptForm, DeptQuery, DeptVO } from '@/api/budget/budgetDept/types';
import { parseTime } from '@/utils/ruoyi';
import { useTableHeight } from "@/hooks/useMethod";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_normal_disable } = toRefs<any>(proxy?.useDict('sys_normal_disable'));

const total = ref(0);
const deptList = ref<DeptVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const deptTableRef = ref<ElTableInstance>();
const queryFormRef = ref<ElFormInstance>();
const deptFormRef = ref<ElFormInstance>();

const initFormData: DeptForm = {
  deptId: undefined,
  deptName: undefined,
  deptCode: undefined,
  status: '0',
  isBusiness: true,
  isManage: true
};
const initData: PageData<DeptForm, DeptQuery> = {
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptName: undefined,
    status: undefined
  },
  rules: {
    deptName: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
    deptCode: [{ required: true, message: '部门编号不能为空', trigger: 'blur' }]
  }
};
const data = reactive<PageData<DeptForm, DeptQuery>>(initData);

const { queryParams, form, rules } = toRefs<PageData<DeptForm, DeptQuery>>(data);

/** 查询部门列表 */
const getList = async () => {
  loading.value = true;
  const res = (await listDept(queryParams.value)).data;
  deptList.value = res.records;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};
/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  deptFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 新增按钮操作 */
const handleAdd = async (row?: DeptVO) => {
  reset();
  dialog.visible = true;
  dialog.title = '添加部门';
};

/** 修改按钮操作 */
const handleUpdate = async (row: DeptVO) => {
  reset();
  const res = await getDept(row.deptId);
  form.value = res.data;
  dialog.visible = true;
  dialog.title = '修改部门';
};
/** 提交按钮 */
const submitForm = () => {
  deptFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      form.value.deptId ? await updateDept(form.value) : await addDept(form.value);
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};
/** 删除按钮操作 */
const handleDelete = async (row: DeptVO) => {
  await proxy?.$modal.confirm('是否确认删除选中的数据项?');
  await delDept(row.deptId);
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};
/**修改状态*/
const updateStatus = async (deptId: number, type: number, status: boolean) => {
  await updateStatusById(deptId, type, status);
  await getList();
};
const tableStyle = useTableHeight(showSearch, {
  withSearch: '61vh',
  withoutSearch: '71vh'
});
onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped></style>
